<?php
/**
 * 发票状态检查类
 *
 * @author xiayuanjun<xiayuanjun@shopex.cn>
 * @version 0.1
 */
class invoice_check{
    
    //检查是否可以新建发票
    public function checkCreate($order_id){
        if(!$order_id){
            return false;
        }
        //取最新发票记录一条，如存在未作废状态(即未开票或者已开票)的记录 则不能新增发票记录
        $mdlInOrder = app::get('invoice')->model('order');
        $arr_filter = array(
            'order_id' => $order_id,
            'is_status|in' => array(0,1),
        );
        $rs_invoice = $mdlInOrder->getList('*', $arr_filter, 0, 1, 'id DESC');
        if($rs_invoice){
            return false;
        }else{
            return true;
        }
    }
    
    //检查是否可以作废/冲红发票
    public function checkCancel($order_id){
        if(!$order_id){
            return false;
        }
        //取相关订单所有发票记录，有且只有一条是未作废状态
        $mdlInOrder = app::get('invoice')->model('order');
        $arr_filter = array(
            'order_id' => $order_id,
            'is_status|in' => array(0,1),
        );
        $rs_invoice = $mdlInOrder->getList('*', $arr_filter);
        if(count($rs_invoice) == 1){
            return $rs_invoice[0];
        }else{
            return false;
        }
    }
    
    //检查是否可以开票
    public function checkBilling($arrBilling){
        $id = $arrBilling["id"];
        $order_id = $arrBilling["order_id"];
        if(!$id || !$order_id){
            return false;
        }
        //取本张发票记录外的此订单是否存在未开票/已开票（即未作废）的发票记录 存在则不能开票
        $mdlInOrder = app::get('invoice')->model('order');
        $arr_filter = array(
            "is_status|in" => array("0","1"),
            "id|noequal" => $id,
            "order_id" => $order_id,
        );
        $rs_info = $mdlInOrder->getList('*', $arr_filter, 0, 1);
        //获取自身id的开票信息 状态必须为未开票
        $self_info = $mdlInOrder->dump(array("id"=>$id));
        if(empty($rs_info) && !empty($self_info) && intval($self_info["is_status"]) == 0){
            return $self_info;
        }else{
            return false;
        }
    }
    
    //从明细如手 检查是否可以开电子发票（开蓝/冲红）
    public function checkEinvoiceCreate($id,$billing_type=1){
        if(!$id){
            return false;
        }
        $mdlInOrderElIt = app::get('invoice')->model('order_electronic_items');
        $rs_item = $mdlInOrderElIt->dump(array("id"=>$id,"billing_type"=>$billing_type));
        if(empty($rs_item)){
            //没有相关明细记录 可以开票
            return true;
        }
        if(!$rs_item["invoice_code"] && !$rs_item["invoice_code"] && $rs_item["serial_no"]){
            //之前点击开票没有成功或者开票中的状态 之前已生产开票流水号 可以开票
            return true;
        }
        //有相关的明细记录 并且已经callback开票成功 不可以开票
        return false;
    }
    
    //检查是否可以编辑
    public function checkEdit($id){
        if(!$id){
            return false;
        }
        //此发票记录 开票状态 必须是未开票
        $mdlInOrder = app::get('invoice')->model('order');
        $rs_info = $mdlInOrder->dump(array("id"=>$id));
        if(intval($rs_info["is_status"]) == 0){
            return true;
        }else{
            return false;
        }
    }
    
    //检查开电子发票打接口的必要参数是否缺失
    public function checkDoEinvoice($rs_invoice){
        $arr_hint = array();
        if(!floatval($rs_invoice["amount"])){
            $arr_hint[] = "开票金额必须大于0";
        }
        if(!floatval($rs_invoice["cost_tax"])){
            $arr_hint[] = "税金必须大于0";
        }
        if(!$rs_invoice["address"]){
            $arr_hint[] = "开票方地址不能为空";
        }
        if(!$rs_invoice["payee_name"]){
            $arr_hint[] = "开票方名称不能为空";
        }
        if(!$rs_invoice["payee_operator"]){
            $arr_hint[] = "开票人不能为空";
        }
        if(!$rs_invoice["tax_no"]){
            $arr_hint[] = "开票方税号不能为空";
        }
        $title = $rs_invoice["tax_company"]?$rs_invoice["tax_company"]:$rs_invoice["title"];
        if(!$title){
            $arr_hint[] = "发票抬头不能为空";
        }
        return array(
            "arr_hint" => $arr_hint,
            "mode" => $rs_invoice["mode"],
        );
    }
    
}